/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-5-23上午09:03:30
 */
package hit.edance.campusmobile.chat.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

/**   
 * 图片处理工具<br/>
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.util
 * @Class PictureUtil
 * @Date 2012-5-23 上午09:03:30
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class PictureUtil {
	
	/**
	 * 圆角比例
	 */
	public static final int ROUNDPROPORTION_DEFAULT = 10; 

	/**
	 * 灰化图片
	 * 
	 * @param bm 彩色的Bitmap
	 * @return 灰化的Bitmap<br/>
	 * 若bm为null，返回null
	 */
	public static Bitmap getGrayBitmap(Bitmap bm){
		if(bm == null){
			return null;
		}
		int width,height;
		height = bm.getHeight();
		width = bm.getWidth();
		
		Bitmap bmGray = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
		Canvas c = new Canvas(bmGray);
		Paint paint = new Paint();
		ColorMatrix cm = new ColorMatrix();
		cm.setSaturation(0);
		ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
		paint.setColorFilter(filter);
		c.drawBitmap(bm, 0, 0,paint);
		return bmGray;
	}
	/**
	 * 将drawable转换为bitmap格式
	 * 
	 * @param drawable drawable图像
	 * @return Bitmap图像<br/>
	 * 若drawable为null，返回null
	 */
	public static Bitmap drawableToBitmap(Drawable drawable){
		if(drawable == null){
			return null;
		}
		int h = drawable.getIntrinsicHeight();
		int w = drawable.getIntrinsicWidth();
		
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				:Bitmap.Config.RGB_565;
		
		Bitmap bitmap = Bitmap.createBitmap(w,h,config);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		drawable.draw(canvas);
		return bitmap;
	}
	/**
	 * 圆角图像
	 * 
	 * @param bitmap bitmap图像
	 * @param roundProportion 圆角比例<br/>
	 * 圆角半径 = 长宽最小值的<b>roundProportion<b/>分之一 
	 * (roundProportion ≥ 2) 
	 * @return 圆角的Bitmap图像<br/>
	 * 若bitmap为null，返回null
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,int roundProportion) {
		if(bitmap == null)
			return null;
		
		if(roundProportion < 2)
			roundProportion = 2;
		
	    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
	        bitmap.getHeight(), Config.ARGB_8888);
	    Canvas canvas = new Canvas(output);

	    final int color = 0xff424242;
	    final Paint paint = new Paint();
	    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
	    final RectF rectF = new RectF(rect);
	    final float roundPx = Math.min(bitmap.getWidth(), bitmap.getHeight())/roundProportion;

	    paint.setAntiAlias(true);
	    canvas.drawARGB(0, 0, 0, 0);
	    paint.setColor(color);
	    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

	    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	    canvas.drawBitmap(bitmap, rect, rect, paint);

	    return output;
	}

	/**
	 * 将字节数组转换为ImageView可调用的Bitmap对象
	 * 
	 * @param bytes
	 * @param opts
	 * @return Bitmap
	 */
	public static Bitmap getPicFromBytes(byte[] bytes,
			BitmapFactory.Options opts) {
		if (bytes != null)
			if (opts != null)
				return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
						opts);
			else
				return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
		return null;
	}

	/**
	 * 图片缩放
	 * 
	 * @param bitmap
	 *            对象
	 * @param w
	 *            要缩放的宽度
	 * @param h
	 *            要缩放的高度
	 * @return newBmp 新 Bitmap对象
	 */
	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return newBmp;
	}
	/**
	 * 图片转换成字符串
	 * 
	 * @param bitmap
	 *            对象
	 * @return newBmp 新 Bitmap对象
	 */
//	public static String getBitmapString(Bitmap bmpimage) {
//		String imageString = null;
//		Bitmap mBitmap = bmpimage;
//		if (mBitmap != null) {
//			Matrix matrix = new Matrix();
//			int mWidth = mBitmap.getWidth();
//			int mHeight = mBitmap.getHeight();
//			float scaleWidth = (float) 150 / mWidth;
//			float scaleHeight = (float) 150 / mHeight;
//			matrix.postScale(scaleWidth, scaleHeight);
//			Bitmap newBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
//					mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
//
//			ByteArrayOutputStream out = new ByteArrayOutputStream();
//			newBitmap.compress(CompressFormat.JPEG, 100, out);
//			byte[] bytes = out.toByteArray();
//			imageString = Base64.encodeToString(bytes, Base64.NO_OPTIONS);
//		}
//		return imageString;
//	}
	/**
	 * BASE64转换成图片
	 * @param imgBase64Str
	 * @return Bitmap图片
	 */
//	public static Bitmap getBitmapByImgBase64Str(String imgBase64Str){  
//	     try {  
//	         byte[] bitmapArray;  
//	         bitmapArray = Base64.decode(imgBase64Str, Base64.NO_OPTIONS);  
//	         return BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);  
//	     } catch (Exception e) {  
//	         e.printStackTrace();  
//	     }  
//	     return null;  
//	  
//	 }
}
